﻿extern alias util;

using System;
using System.Diagnostics.CodeAnalysis;

using util::JetBrains.Application.Progress;
using JetBrains.ProjectModel;
using JetBrains.ReSharper.Feature.Services.Bulbs;
using JetBrains.ReSharper.Intentions;
using JetBrains.TextControl;
using util::JetBrains.Util;

namespace FinlogiK.ReSharperContrib
{
	[SuppressMessage("Microsoft.Performance", "CA1812:AvoidUninstantiatedInternalClasses", Justification = "Instantiated by reflection.")]
    [QuickFix]
    internal sealed class AddStaticQualifierQuickFix : BulbItemImpl, IQuickFix
	{
		#region Fields

		private readonly AddStaticQualifierHighlight _highlight;

		#endregion

		#region Constructors

		/// <summary>
		/// 	Initializes a new instance of the <see cref = "AddStaticQualifierQuickFix" /> class.
		/// </summary>
		/// <param name = "highlight">The highlight related to the quick-fix.</param>
		public AddStaticQualifierQuickFix(AddStaticQualifierHighlight highlight)
		{
			if (highlight == null) throw new ArgumentNullException("highlight");

			this._highlight = highlight;
		}

		#endregion

		#region Properties

	    /// <summary>
	    /// Executes QuickFix or ContextAction. Returns post-execute method.
	    /// </summary>
	    /// <returns>
	    /// Action to execute after document and PSI transaction finish. Use to open TextControls, navigate caret, etc.
	    /// </returns>
	    protected override Action<ITextControl> ExecutePsiTransaction(ISolution solution, IProgressIndicator progress)
	    {
            ArrangeStaticQualifierHelper.AddStaticQualifier(this._highlight.ReferenceExpression);

            return null;
	    }

	    public override string Text
		{
			get
			{
				return "Add static qualifier";
			}
		}

		#endregion

		#region IQuickFix Members

		public bool IsAvailable(IUserDataHolder cache)
		{
			return this._highlight.IsValid();
		}

		#endregion
	}
}